<div class="layuimini-container layui-anim layui-anim-upbit">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">部门编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="departmentcode" id="departmentcode" autocomplete="off"
                                    class="layui-input" id="jobNumber">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">部门名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="departmentname" id="departmentname" autocomplete="off"
                                    class="layui-input" id="userName">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                lay-filter="data-search-btn" style="margin-left:10px;width:90px;height:38px;border:none;outline:none;border-radius:6px;background-color:#007bff;color:white"><i
                                    class="layui-icon"></i> 搜 索</button>


                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <br>
        <div id="table" lay-filter="currentTableFilter">
            <div class="layui-btn-container" style="position: relative;">
                <!-- <button class="layui-btn layui-btn-sm data-add-btn" id="tongbuUser" > 同步部门 </button>
                <button class="layui-btn layui-btn-sm data-add-btn" id="addUser" style="display:none"> 添加部门 </button> -->
                <!--<button class="layui-btn layui-btn-sm layui-btn-warm add-dept addInfo" id=""> 创建部门 </button>-->
                <!--<button class="layui-btn layui-btn-sm layui-btn-warm template-download" id=""> 模板下载 </button>-->
                <!--<button class="layui-btn layui-btn-sm layui-btn-warm batch-import" id="upload"> 批量导入 </button>-->

                <button type="button" class="layui-btn layui-btn-warm layui-btn-radiusadd-dept addInfo"
                        style="border-radius: 2px;"><i class="layui-icon layui-icon-form"></i> 添加部门</button>
                <button type="submit" lay-submit="" class="layui-btn layui-btn-warm template-download"
                                    style="border-radius:6px;background-color:#26B99A;">
                                    <i class="fa fa-cloud-download"></i> 模板下载</button>
                <button type="submit" lay-submit="" class="layui-btn layui-btn-warm batch-import" id="upload"
                                    style="border-radius:6px;background-color:#1e9fff">
                                    <i class="fa fa-cloud-upload"></i> 批量导入</button>
            </div>
        </div>
        <div id="page"></div>
    </div>
</div>

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-xs layui-btn-normal data-count-edit" lay-event="editInfo">编辑</a>
    <!--<a class="layui-btn layui-btn-xs layui-btn layui-btn-normal data-count-edit" lay-event="" id="editTable">选择负责人</a>-->
    <a class="layui-btn layui-btn-xs layui-btn-danger data-delete-btn"  lay-event="deleteInfo">删除</a>
</script>


<script>
    var tableBar = [
        { title: '序 号', type: 'numbers', width: '10%', align: "center" },
        { field: 'departmentCode', title: '部 门 编 号', width: '10%', align: "center" },
        { field: 'departmentName', title: '部 门 名 称', width: '15%', align: "center" },
        { field: 'leaderNames', title: '部 门 领 导', width: '20%', align: "center" },
        { field: 'pdepartmentCode', title: '上 级 部 门 编 号', width: '15%', align: "center" },
        { field: 'pdepartmentName', title: '上 级 部 门 名 称', align: 'center', width: '15%' },
        { title: '操 作', width: '15%', templet: '#currentTableBar', align: "center" }
    ]

    var initTableParams = {
        "departmentcode": "",
        "departmentname": "",
        "orderType": "部门编号",
        "orderValue": "asc",
        "page": 1,
        "pageSize": 10
    }


    //封装 表格渲染方法
    function tableRender(tableBar, initTableParams) {
        layuiEx.tableRender(
            "/adminDepartmentInfo/departmentlist",
            'table',
            'page',
            tableBar,
            initTableParams
        );
    }

    tableRender(tableBar, initTableParams)

    //检索事件
    layui.form.on('submit(data-search-btn)', function (params) {
        console.log(params)
        layuiEx.tableRender(
            "/adminDepartmentInfo/departmentlist",
            'table',
            'page',
            tableBar,
            {
                "departmentcode": params.field.departmentcode,
                "departmentname": params.field.departmentname,
                "orderType": "部门编号",
                "orderValue": "asc",
                "page": 1,
                "pageSize": 10
            }
        );
        return false
    })

    $(".addInfo").on("click", function (obj) {
        loadAddDepartmentPage("add")
    });

    // 监听编辑和删除
    layui.table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'editInfo') {
            loadAddDepartmentPage("edit", data)
        } else if (obj.event === 'deleteInfo') {
            layer.confirm('确定删除 ' + obj.data.departmentName + " ?", function (index) {
                deleteDepartment(data)
            })
        }
    });

    function loadAddDepartmentPage(addORedit, data) {
        var content = layui.miniPage.getHrefContent('mypages/usual/departmentMgt/addDepartment.html');
        var openWH = layui.miniPage.getOpenWidthHeight();
        var title = ""
        if (addORedit == "add") {
            title = "添加部门"
        } else {
            title = "编辑部门"
        }
        //不能用 var 前缀
        data_departmentMgr = data
        var index = layer.open({
            title: title,
            type: 1,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            offset: '100px',
            area: ['500px', 'auto'],
            content: content,
            end: function () {
                var trunToPage = 1
                if (addORedit == "add") {

                } else {
                    trunToPage = $(".layui-laypage-em").next().html()
                }
                layuiEx.tableRender(
                    "/adminDepartmentInfo/departmentlist",
                    'table',
                    'page',
                    tableBar,
                    {
                        "departmentcode": $("#departmentcode").val(),
                        "departmentname": $("#departmentname").val(),
                        "orderType": "部门编号",
                        "orderValue": "asc",
                        "page": trunToPage,
                        "pageSize": 10
                    }
                );
            }
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
        return false;
    }

    //删除逻辑
    function deleteDepartment(data) {
        id = data.id
        shared.ajaxPost('/adminDepartmentInfo/deleteDepartment', { "id": data.departmentCode }, function callback(res) {
            if (res) {
                layer.msg(res, {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    layuiEx.tableRender(
                        "/adminDepartmentInfo/departmentlist",
                        'table',
                        'page',
                        tableBar,
                        {
                            "departmentcode": $("#departmentcode").val(),
                            "departmentname": $("#departmentname").val(),
                            "orderType": "部门编号",
                            "orderValue": "asc",
                            "page": 1,
                            "pageSize": 10
                        }
                    );
                });
            }
        })
    }


    /**
    下载部门导入模板
    */
    $(".template-download").on("click", function (obj) {
        var url = config.server + "/adminDepartmentInfo/getTemplate?tpType=depBatchImp";
        window.location.href = url;
    });


    /*
    上传批量导入部门的excel
    */
    layui.upload.render({ //允许上传的文件后缀
        headers: { token: shared.getCookie('token') },
        elem: '#upload'
        , url: config.server + '/adminDepartmentInfo/handleBatchImportDpt'
        , before: function (obj) {
            // this.data = { 'dbCode': $("#dbCode").val().split("_")[0] };
            // showloading(true);
        }
        , accept: 'file' //普通文件
        , exts: 'xlsx' //只允许上传压缩文件
        , size: 1050000//设置文件最大可允许上传的大小1G，单位 KB。不支持ie8/9
        , done: function (res) {
            if (res.code != 200) {
                layer.confirm(res.msg, {
                    btn: ['确定'], icon: 2, title: '信息'
                }, function () {
                    layer.closeAll();
                });
            }
            else {
                // 刷新页面
                layer.msg(res.msg, {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    layer.closeAll();
                    layuiEx.tableRender(
                        "/adminDepartmentInfo/departmentlist",
                        'table',
                        'page',
                        tableBar,
                        {
                            "departmentcode": $("#departmentcode").val(),
                            "departmentname": $("#departmentname").val(),
                            "orderType": "部门编号",
                            "orderValue": "asc",
                            "page": 1,
                            "pageSize": 10
                        }
                    );
                });

            }
        }, error: function () {
        }

    });
</script>